package com.hzk.validator;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hzk.utils.ValidatorUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @auoter:jackh
 */

public class IsMobileValidator implements ConstraintValidator<IsMobile,String> {
    private boolean required = false;

    @Override
    public void initialize(IsMobile constraintAnnotation) {
         required = constraintAnnotation.required();
    }
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
         if (required){
             return ValidatorUtils.isMobile(value);
       }else {
             if (StringUtils.isEmpty(value)){
                  return true;
             }else {
                  return ValidatorUtils.isMobile(value);
             }
       }
    }
}
